package com.lms.dao.update;

import com.lms.bean.User;
import com.lms.util.Constants;
import org.apache.commons.codec.digest.DigestUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UpdataDaoImpl implements UpdateDao{
    public ResultSet rs=null;
    public PreparedStatement pstm=null;
    @Override
    public void save(List<User> list, Connection connection) {
        if (list!=null && connection!=null){
            try {
                String sql="insert into user_account(userCode, userName, userPassword, userRole) values (?,?,?,?)";
                pstm = connection.prepareStatement(sql);
                for (User user:list){
                    pstm.setObject(1,user.getUserCode());
                    pstm.setObject(2,user.getUserName());
                    pstm.setObject(3, DigestUtils.md5Hex(Constants.SALT_HEAD+user.getUserPassWord()+Constants.SALT_FOOT));
                    pstm.setObject(4,user.getUserRole());
                    pstm.addBatch();
                }
                pstm.executeBatch();
            } catch (SQLException e) {
                System.out.println("======本次导入的数据可能存在与数据库重复的成分，重复部分已被拒绝=====");
                e.printStackTrace();
            }finally {
                try {
                    connection.close();
                    pstm.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
